import os
from os import listdir, chdir
from os.path import isfile, join
import sys
import subprocess

# Define colors for printing
class colors:
	PASS = '\x1b[42m'
	FAIL = '\x1b[41m'
	BOLD = '\033[1m'
	RESET = '\033[0;0m'

keyword = ''
if ('--run_test' in sys.argv):
	keyword = sys.argv[sys.argv.index('--run_test')+1]

# Get list of existing workloads
path = './workloads/'
workloads = [f for f in listdir(path) if isfile(join(path, f))]
workloads = [f for f in workloads if keyword in f]
workloads.sort()
for i in range(len(workloads)):
	workloads[i] = workloads[i].split('.')[0]

chdir('../compiler')
for workload in workloads:
	subprocess.call(['cp', '../scripts/workloads/'+workload+'.py', './'], shell=False)
	sys.stdout.write('{:<35}'.format(workload) + '\n')
	sys.stdout.flush()

	outfile = open('../scripts/reports/'+workload+'_fsim.rpt', 'w')
	subprocess.call(['python', workload+'.py'], stdout=outfile, shell=False)

	rptfile = open('../scripts/reports/'+workload+'_fsim.rpt', 'r')
	parse_fsim_res = False
	for line in rptfile:
		if 'Simulation finished successfully' in line:
			parse_fsim_res = True
	if(not parse_fsim_res):
		print(colors.FAIL + 'FAIL' + colors.RESET)

	subprocess.call(['rm', workload+'.py'], shell=False)

	subprocess.call(['cp', './model.onnx', '../scripts/reports/'+workload+'.onnx'], shell=False)

